<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeTool :tree-node="company" :is-root="true" @addDept="addDept" />
      </el-card>

      <el-tree :data="departs" :props="defaultProps" @node-click="() => null">
        <template v-slot="{data}">
          <TreeTool :tree-node="data" @addDept="addDept" @delDepartments="getDepartment" @editDep="editDep" />
        </template>
      </el-tree>
    </div>
    <AddDept ref="addDept" :show-dialog.sync="isShowDialog" :tree-node="node" @getDep="getDepartment" />
  </div>
</template>

<script>
import TreeTool from './components/tree-tools'
import AddDept from './components/add-dept'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'

export default {
  components: {
    TreeTool,
    AddDept
  },
  data() {
    return {
      loading: false, // 界面加载动画
      isShowDialog: false, // 是否展示新增的弹框
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人' },
      defaultProps: {
        label: 'name'
      },
      departs: [
        { name: '总裁办', manager: '曹操', children: [{ name: '董事会', manager: '曹丕' }] },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }
      ],
      node: {}
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    editDep(treeNode) {
      // 记录当前点击的节点的数据
      this.node = treeNode
      // console.log(this.$refs)
      // 展示弹层
      this.isShowDialog = true
      console.log(treeNode.id)
      //  调用子组件弹层数据
      this.$refs.addDept.getDetailById(treeNode.id)
    },
    addDept(treeNode) {
      console.log(treeNode)
      this.node = treeNode
      this.isShowDialog = true
    },
    async getDepartment() {
      this.loading = true
      const { companyName, depts } = await getDepartments()
      this.company = {
        name: companyName,
        manager: '负责人',
        id: ''
      }
      this.departs = tranListToTreeData(depts, '') // 需要将其转化成树形结构
      this.loading = false
      // console.log(res)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>

